<!-- 完成维修-维修操作 -->
<template>
	<ListLayout>
		<!-- 维修情况 -->
		<MaintenanceHandleCard ref="MaintenanceHandleCard" :showHandleCompleteMaintenance="ShowHandleCompleteMaintenance"></MaintenanceHandleCard>
		<!-- 维修信息处理 -->
		<RepairHandleCard ref="RepairHandleCard"></RepairHandleCard>
		<!-- 发票信息处理 -->
		<InvoiceHandleCard ref="InvoiceHandleCard"></InvoiceHandleCard>
		<!-- 测试报告 -->
		<ReportTestHandleCard ref="ReportTestHandleCard"></ReportTestHandleCard>
		<!-- 质保 -->
		<WarrantyHandleCard ref="WarrantyHandleCard"></WarrantyHandleCard>

		<template #footer>
			<HButtonGroup @rightClick="rightClick" @leftClick="leftClick">
				<template #left>
					取消
				</template>
				<template #right>
					确认完成维修
				</template>
			</HButtonGroup>
		</template>
	</ListLayout>
</template>

<script>
	import ListLayout from '@/components/layout/listLayout.vue'
	import MaintenanceHandleCard from '@/components/card/sendRepair/factory/info/handleMaintenance.vue'
	import RepairHandleCard from '@/components/card/sendRepair/factory/info/handleRepair.vue'
	import InvoiceHandleCard from '@/components/card/sendRepair/factory/info/handleInvoice.vue'
	import ReportTestHandleCard from '@/components/card/sendRepair/factory/info/handleTestReport.vue'
	import WarrantyHandleCard from '@/components/card/sendRepair/factory/info/handleWarranty.vue'

	import HButtonGroup from '@/components/buttonGroup/groupTwo.vue'
	import { factoryState } from '@/sendRepair/factory/state.js'
	import { attachmentFormatter } from '@/common/submitFormatter.js'
	import ReapirApi from '@/apis/repair.js'
	import SendRepairRouterConfig from '@/sendRepair/routerConfig.js'


	export default {
		components: {
			ListLayout,
			MaintenanceHandleCard,
			RepairHandleCard,
			InvoiceHandleCard,
			ReportTestHandleCard,
			WarrantyHandleCard,
			HButtonGroup
		},
		data() {
			return {
				stateParams: {
					color: factoryState.colors,
					text: '',
					state: ''
				},
				routerParams: {
					id: ''
				}
			}
		},

		computed: {
			// 显示完成维修 维修情况栏
			ShowHandleCompleteMaintenance() {
				return this.stateParams.state == factoryState.stateValue.UnderRepair
			}
		},
		async onLoad(query) {
			const {
				state,
				currStateToName,
				id
			} = query
			this.$set(this.stateParams, 'text', currStateToName)
			this.$set(this.stateParams, 'state', state)
			this.$set(this.routerParams, 'id', id)
			console.log(query, 'onLoad---query')
		},

		methods: {

			leftClick() {
				wx.navigateBack()
			},
			async rightClick() {
				let {
					MaintenanceHandleCard,
					RepairHandleCard,
					InvoiceHandleCard,
					ReportTestHandleCard,
					WarrantyHandleCard,
				} = this.$refs;
				if (InvoiceHandleCard.validate() && WarrantyHandleCard.validate()) {
					let {
						deviceState,
						deviceData,
						remark,
						enclosure,
						process,
						faultReason,
					} = MaintenanceHandleCard.$data.formData

					let {
						repairList
					} = RepairHandleCard.$data
					let {
						invoiceType,
						invoiceNum,
						invoiceAttachment,
						isVoice
					} = InvoiceHandleCard.$data.formData
					let {
						testReportFlag,
						testReportEnclosure
					} = ReportTestHandleCard.$data.formData
					let {
						warrantyFlag,
						warranty,
						warrantyStartTime
					} = WarrantyHandleCard.$data.formData

					let params = {
						// 送修id
						repairId: this.routerParams.id,
						// 维修结果（ 1. 已修好 2. 未修好 3. 厂家无法维修， 4. 科室放弃维修， 5. 厂家放弃维修， 6. 报价审核不通过）
						repairResult: '',
						// 设备数据状态
						deviceData: deviceData,
						// 是否有发票（ 0. 否， 1. 是）
						invoiceFlag: isVoice ? 1 : 0,
						// 发票类型
						invoiceType: invoiceType,
						// 发票号
						invoiceNum: invoiceNum,
						// 发票附件
						invoiceEnclosure: attachmentFormatter(invoiceAttachment),
						// 是否有测试报告（ 0. 否， 1. 是）
						testReportFlag: testReportFlag ? 1 : 0,
						// 测试报告附件
						testReportEnclosure: testReportEnclosure,
						// 备注
						remark: '',
						// 处理过程
						process: process,
						// 故障原因
						faultReason: faultReason,
						// 附件
						enclosure: enclosure,
						// 维修项目详情 [{"name":"维修项测试","num":"5"},{"name":"维修项测试2","num":"20"}]	
						maintenanceItem: JSON.stringify(repairList),
						// 是否继续维修（0.否，1.是）
						continueRepairFlag: '',
						// 结算方式
						settlementMethod: '',
						// 设备状态外观
						deviceState: deviceState,
						// 有无质保（ 0. 无， 1. 有）
						warrantyFlag: warrantyFlag ? 1 : 0,
						// 质保期（ 月）
						warrantyPeriod: warranty,
						// 质保开始时间
						warrantyStartTime: warrantyStartTime,
					}
					await ReapirApi.department.confirmCompleteRepair(params)

					wx.showToast({
						title: '提交成功',
						icon: 'success',
						complete() {
							wx.redirectTo({
								url: SendRepairRouterConfig.Factory.Index
							})
						}
					})
				}
			}
		}

	}
</script>

<style>
</style>
